package cz.idsv.web.mvc.controller;

/**
 * Shop Controller.
 * 
 * @author Jan Borzensky
 */
import java.util.Collection;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import cz.idsv.web.mvc.model.entity.ShopEntity;
import cz.idsv.web.mvc.model.service.ShopService;



/**
 * Shop controller.
 * 
 * @author Jan Borzensky
 */
@Controller
@SessionAttributes(value = {"shopEntity"})
public class ShopController {
	
	private static final String LIST_MODEL_KEY = "list";
	private static final String SHOP_ENTITY = "shopEntity";
	
	protected static Logger logger = Logger.getLogger("ShopController"); 
	
	@Autowired()
	private ShopService shopService;
	
	/**
     * <p>Searches for all product and returns them in a 
     * <code>Collection</code>.</p>
     * 
     * <p>Expected HTTP GET and request '/address/list'.</p>
     */
    @RequestMapping(value="/shop/list", method=RequestMethod.GET)
    public @ModelAttribute(LIST_MODEL_KEY) Collection<ShopEntity> search() {
    	logger.debug("Received request to show all shop"); 
    	
        return shopService.findShop();
    }
    
	/**
     * <p>Create new or update old shop 
     * <code>AddressEntity</code>.</p>
     * 
     * <p>Expected HTTP GET and request '/shop/form'.</p>
     */
    @RequestMapping(value="/shop/form", method=RequestMethod.GET)
    public @ModelAttribute(SHOP_ENTITY)  ShopEntity getDocumentForm(@RequestParam(value = "id", required = false) Integer id) {
        if(id==null)return new ShopEntity();
        return shopService.findShopById(id);
    }
    
	/**
     * <p>Save new or update old shop 
     * <code>ShopEntity</code>.</p>
     * 
     * <p>Expected HTTP POST and response '/shop/saveForm'.</p>
     */
    @RequestMapping(value="/shop/saveForm", method=RequestMethod.POST)
    public String saveShopForm(ShopEntity shopEntity, Model model) {
    	if(shopEntity != null) 
    		shopService.save(shopEntity);
    	return "redirect:/shop/list.html";
    }
    
    /**
     * <p>Deletes a shop.</p>
     * 
     * <p>Expected HTTP POST and request '/address/delete'.</p>
     */
    @RequestMapping(value="/shop/delete", method=RequestMethod.POST)
    public String delete(ShopEntity shopEntity) {
    	logger.debug("Received request to delete shop with id:"+ shopEntity.getId());
        shopService.delete(shopEntity);
        return "redirect:/shop/list.html";
    }
    
}
